// const { SM4 } = require('gm-crypto')
const formatTime = date => {
  const year = date.getFullYear()
  const month = date.getMonth() + 1
  const day = date.getDate()
  const hour = date.getHours()
  const minute = date.getMinutes()
  const second = date.getSeconds()

  return `${[year, month, day].map(formatNumber).join('/')} ${[hour, minute, second].map(formatNumber).join(':')}`
}

const formatNumber = n => {
  n = n.toString()
  return n[1] ? n : `0${n}`
}

/**
 * 16 位 0-9 随机数
 * @returns
 */
function generateRandomNumber() {
  let result = '';
  let characters = '0123456789';
  let charactersLength = characters.length;

  for (let i = 0; i < 16; i++) {
    let randomIndex = Math.floor(Math.random() * charactersLength);
    result += characters.charAt(randomIndex);
  }

  return result;
}
/**
 * 获取 26 个字母数组
 * @returns array
 */
function getAlphabetArray(count = 26) {
  let alphabet = [];
  let startCharCode = 'a'.charCodeAt(0); // 获取字母 a 的 Unicode 编码

  for (let i = 0; i < count; i++) {
    let char = String.fromCharCode(startCharCode + i);
    alphabet.push(char);
  }

  return alphabet;
}

/**
 * 根据随机数字生成对应字母，例如 0->a, 1->b, 2->c
 */
function getRandomNumToAlphabet(randomNumber) {
  if (randomNumber) {
    const alphabet = getAlphabetArray(10);
    const nums = randomNumber.split('');
    const list = [];
    nums.forEach((num) => {
      list.push(alphabet[+num]);
    });

    return list.join('');
  }

  return randomNumber;
}


function utf8ToHexString(input) {
  let result = '';
  for (let i = 0; i < input.length; i++) {
    let code = input.charCodeAt(i);
    let hcode = code.toString(16);
    result += hcode;
  }
  return result;
}

function getEncryptedData(data, key){
  // return SM4.encrypt(data, key, {
  //   iv: utf8ToHexString('0000000000000000'),
  //   mode: SM4.constants.CBC,
  //   inputEncoding: 'utf8',
  //   outputEncoding: 'hex',
  // });
};

/* 获取验签参数 */
function getSignParam (openid) {
  // const timestamp = Math.floor(new Date().getTime() / 1000);
  // const randKey = generateRandomNumber();
  // const key = getRandomNumToAlphabet(randKey);
  // const $str = `openid=${openid}&randKey=${randKey}&timestamp=${timestamp}`;
  // const sign = getEncryptedData($str, utf8ToHexString(key));

  // return {
  //   timestamp,
  //   randKey,
  //   sign: sign,
  // };
};

function getUrlParams(url, prop) {
  // 创建一个对象用于存储参数键值对
  const params = {};

  // 如果传入的是 URL 字符串，则解析其查询部分
  const queryString = url.split('?')[1];

  if (queryString) {
      // 将查询字符串按 '&' 分割成多个键值对
      const pairs = queryString.split('&');

      for (let pair of pairs) {
          // 将每个键值对按 '=' 分割
          const [key, value] = pair.split('=');

          // 使用 decodeURIComponent 解码参数值，并保存到对象中
          params[decodeURIComponent(key)] = decodeURIComponent(value || '');
      }
  }

  return params[prop];
};

module.exports = {
  formatTime,
  utf8ToHexString,
  getEncryptedData,
  generateRandomNumber,
  getAlphabetArray,
  getRandomNumToAlphabet,
  getSignParam,
  getUrlParams
}
